using System;

namespace Lucene.Linq.VSTests.Helpers
{
    public class RandomStringGenerator {
        private Random _r;
        const string Uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        const string Lowercase = "abcdefghijklmnopqrstuvwxyz";
        const string Numbers = "0123456789";
        const string Symbols = @"~`!@#$%^&*()-_=+<>?:,./\[]{}|'";

        public RandomStringGenerator(Random random) {
            _r = random;
        }

        public RandomStringGenerator() {
            _r = new Random();
        }

        public RandomStringGenerator(int seed) {
            _r = new Random(seed);
        }

        public virtual string NextString(int length) {
            return NextString(length, true, true, true, true);
        }

        public virtual string NextString(int length, bool lowerCase, bool upperCase, bool numbers, bool symbols) {
            var charArray = new char[length];
            string charPool = string.Empty;

            //Build character pool
            if (lowerCase)
                charPool += Lowercase;

            if (upperCase)
                charPool += Uppercase;

            if (numbers)
                charPool += Numbers;

            if (symbols)
                charPool += Symbols;

            //Build the output character array
            for (int i = 0; i < charArray.Length; i++) {
                //Pick a random integer in the character pool
                int index = _r.Next(0, charPool.Length);

                //Set it to the output character array
                charArray[i] = charPool[index];
            }

            return new string(charArray);
        }
    }
}